package com.dev.ac.service.impl;

import cn.hutool.core.util.IdUtil;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.dev.ac.common.StudentTemplate;
import com.dev.ac.service.AUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author 付
 * @Date 2024-04-14 19:13
 * @Version 1.0
 * @description
 * @since 1.0
 */
@Service
@Slf4j
public class FileServiceImpl {
	@Autowired
	AUserService aUserService;
	private static final String filePath = System.getProperty("user.dir") + "/file/";

	public boolean uploadWord(MultipartFile file, HttpServletRequest request) {
		// 获取文件唯一标识名称
		String flag = IdUtil.fastSimpleUUID();
		// 获取文件源名称
		String oldFileName = file.getOriginalFilename();
		// 获取后缀
		String fileType = oldFileName.substring(oldFileName.lastIndexOf("."));
		// 新的文件名称
		String newFileName = flag + fileType;
		File wordDir = new File(filePath);
		if (!wordDir.exists()) {
			// 文件是否存在,不存在就创建
			wordDir.mkdirs();
		}
		try {
			// 文件上传
			file.transferTo(new File(wordDir, newFileName));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return false;
	}

	public Object download(String fileName, HttpServletResponse response) {
		// 设置Word文档的URL地址
		String fileUrl = "file:///" + filePath + fileName+".docx";
		// 设置保存到本地的文件路径
		String savePath = "D:/" + fileName+".docx";

		try {
			// 创建URL对象
			URL url = new URL(fileUrl);
			// 打开连接
			URLConnection conn = url.openConnection();
			// 设置请求头
			conn.setRequestProperty("User-Agent", "Mozilla/5.0");
			// 获取输入流
			InputStream inputStream = conn.getInputStream();

			File file = new File(savePath);
			if (!file.exists()) {
				file.createNewFile();
			}

			// 创建输出流
			OutputStream outputStream = new FileOutputStream(savePath);


			// 读取和写入数据
			byte[] buffer = new byte[1024];
			int length;
			while ((length = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, length);
			}

			// 关闭连接
			outputStream.close();
			inputStream.close();
			return savePath;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


	public void uploadExcel(MultipartFile file, HttpServletResponse response) {
		List<StudentTemplate> students = new ArrayList<>();
		try (InputStream inputStream = file.getInputStream()) {
			Workbook workbook = new XSSFWorkbook(inputStream);
			Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个sheet
			for (Row row : sheet) {
				if (row.getRowNum() == 0) {
					// 跳过标题行
					continue;
				}
				double id = row.getCell(0).getNumericCellValue();// 编号
				Long username = (long) row.getCell(1).getNumericCellValue();// 学号
				String nickname = row.getCell(2).getStringCellValue();// 姓名
				String department = row.getCell(3).getStringCellValue();// 学院
				String address = row.getCell(4).getStringCellValue();// 地址
				String email = row.getCell(5).getStringCellValue();// 邮箱
				String school = row.getCell(5).getStringCellValue();// 学校
				StudentTemplate studentTemplate = new StudentTemplate(id, username, nickname, department, address, email, school);

				students.add(studentTemplate);
			}
			System.out.println("数据-->" + students);

			aUserService.batchInsertUser(students);
			inputStream.close();
		} catch (IOException e) {
			log.error("出错了--->{}", e);
			e.printStackTrace();
		}
	}


}
